package com.hhlkeepalive.utils;

import android.app.Activity;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */
public class ActivityManager {

    private static List<ActivityBean> activityStack = new ArrayList<>();

    private ActivityManager() {
    }
    //添加到销毁队列
    public static void addDestroyActivity(Activity activity, String activityName) {
        activityStack.add(ActivityBean.getInstance(activityName,activity));
    }

    //销毁指定Activity
    public static void destroyActivity(String activityName) {
        int size=activityStack.size();
        for (int i = 0; i < size; i++) {
            if(activityName.equals(activityStack.get(i).getName())){
                activityStack.get(i).getActivity().finish();
                activityStack.remove(i);
                return ;
            }
        }
    }
    //销毁指定Activity
    public static void destroyByContains(String activityName) {
        int size=activityStack.size();
        for (int i = 0; i < size; i++) {
            if(activityStack.get(i).getName().endsWith(activityName)){
                activityStack.get(i).getActivity().finish();
                activityStack.remove(i);
                return ;
            }
        }
    }
    //是否包含指定Activity
    public static boolean containsActivity(String activityName) {
        int size=activityStack.size();
        for (int i = 0; i < size; i++) {
            if(activityStack.get(i).getName().endsWith(activityName)){
                return true;
            }
        }
        return false;
    }

    //销毁其他所有Activity
    public static void destroyOtherActivity(String activityName) {
        int size=activityStack.size();
        for (int i = size-1; i >=0; i--) {
            if(!activityStack.get(i).getName().endsWith(activityName)){
                activityStack.get(i).getActivity().finish();
                activityStack.remove(i);
            }
        }
    }

    //指定数目回退activity
    public static void destroyActivityIndex(int count){
        int size=activityStack.size();
        if (size >= count) {
            for (int i = size - 1; i > size - 1 - count; i--) {
                activityStack.get(i).getActivity().finish();
                activityStack.remove(i);
            }
        }
    }

    public static ActivityBean getCurrRunActivity(){
        return activityStack.get(activityStack.size()-1);
    }

    public  static  String getCurrActivity(){

      return   activityStack.toString();

    }
}
